package xddq

import (
	"fmt"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

// 判断是否需要砍价
func (xd *XdDriver) CheckBargainingProgressInfoData(info D.BargainingProgressInfo) {
	xd.StopTaskByID(proto.S_CUT_PRICE_SYNC)
	if info.Time.Sub(time.Now()) < 3*time.Hour && info.CutPrice > 2888 {
		log.Infof("开始妖盟-珍宝阁购买, 目前总砍价%d仙玉, 已砍价%d人，未砍价%d人", info.CutPrice, len(info.Negotiated), len(info.UnPriced))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_CUT_PRICE_BUY, fmt.Sprintf("08%s", info.ID)))
	}
	hasCutPrice := false
	//检查是否是同一个妖盟
	for _, people := range info.Negotiated {
		if xd.checkIsAdminUserID(people.ID) {
			hasCutPrice = true
			break
		}
	}
	for _, people := range info.UnPriced {
		if xd.checkIsAdminUserID(people.ID) {
			hasCutPrice = true
			break
		}
	}
	if !hasCutPrice && len(info.Negotiated) == 0 && len(info.UnPriced) == 0 {
		log.Error("这位道友，你跟我不在一个修仙门派！咱们的道理差得可远了。别想偷我的法宝，快去找你自己的秘籍认真修炼吧，别再想着那些歪门邪道了！要不然，等我飞升的时候，可别哭着求我带你！")
		//xd.cancel()
		log.Warnf("%+v", info)
	} else {
		hasCutPrice = false
	}
	for _, people := range info.Negotiated {
		if people.ID == xd.UserData.UserID {
			hasCutPrice = true
		}
	}
	if !hasCutPrice {
		log.Infof("开始妖盟-珍宝阁砍价, 目前总砍价%d仙玉, 已砍价%d人，未砍价%d人", info.CutPrice, len(info.Negotiated), len(info.UnPriced))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_CUT_PRICE_BARGAIN, fmt.Sprintf("08%s", info.ID)))
		if xd.StartSendCmd {
			//仙居协助
			for _, people := range info.Negotiated {
				if people.ID != xd.UserData.UserID {
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_TREE_HELP, fmt.Sprintf("08%s", people.ID)))
					time.Sleep(1 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_GIVE_LIKE, fmt.Sprintf("08 %s 10 01", people.ID)))
					time.Sleep(1 * time.Second)
				}
			}
			for _, people := range info.UnPriced {
				if people.ID != xd.UserData.UserID {
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_TREE_HELP, fmt.Sprintf("08%s", people.ID)))
					time.Sleep(1 * time.Second)
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_GIVE_LIKE, fmt.Sprintf("08 %s 10 01", people.ID)))
					time.Sleep(1 * time.Second)
				}
			}
		}
		return
	}
	percentage := len(info.Negotiated) * 100 / (len(info.Negotiated) + len(info.UnPriced)) //砍价人数比例
	if !xd.UserData.HasBuyZhenBao && info.CutPrice > 2800 && (percentage > 92 || len(info.UnPriced) <= 3) {
		log.Infof("开始妖盟-珍宝阁购买, 目前总砍价%d仙玉, 已砍价%d人，未砍价%d人", info.CutPrice, len(info.Negotiated), len(info.UnPriced))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_CUT_PRICE_BUY, fmt.Sprintf("08%s", info.ID)))
		xd.UserData.HasBuyZhenBao = true
		if xd.StartSendCmd {
			//仙居协助
			for _, people := range info.Negotiated {
				if people.ID != xd.UserData.UserID {
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_TREE_HELP, fmt.Sprintf("08%s", people.ID)))
					time.Sleep(1 * time.Second)
				}
			}
			for _, people := range info.UnPriced {
				if people.ID != xd.UserData.UserID {
					xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YARDPB_TREE_HELP, fmt.Sprintf("08%s", people.ID)))
					time.Sleep(1 * time.Second)
				}
			}
		}
	}
}

// 判断妖盟悬赏护送信息
func (xd *XdDriver) CheckDemonLeagueInfoData(info D.DemonLeagueInfo) {
	if info.NeedTake {
		log.Infof("妖盟悬赏-开始护送%s", D.GetDemonLeagueOfferGradeDesc(info.Grade))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_DEAL_BOUNTY, fmt.Sprintf("08 %s 10 01", info.ID)))
		return
	}
	if info.Taking {
		log.Infof("妖盟悬赏-正在护送%s", D.GetDemonLeagueOfferGradeDesc(info.Grade))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_DEAL_BOUNTY, fmt.Sprintf("08 %s 10 01", info.ID)))
		return
	}
	if info.NeedClaim {
		log.Infof("妖盟悬赏-领取护送%s奖励", D.GetDemonLeagueOfferGradeDesc(info.Grade))
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_GET_REWARD_ESCORT, ""))
		xd.ResetTaskByID(proto.S_UNION_BOUNTY_OPEN_BOUNTY_EVENT)
		return
	}
	if info.Done { //全部护送完成
		log.Info("妖盟-悬赏任务全部完成")
		xd.StopTaskByID(proto.S_UNION_BOUNTY_OPEN_BOUNTY_EVENT)
		xd.StopTaskByID(proto.S_UNION_BOUNTY_OPEN_MONSTER)
		return
	}
	log.Infof("妖盟悬赏-刷新怪物悬赏情况")
	xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_OPEN_MONSTER, fmt.Sprintf("08 %s", xd.UserData.UserID)))
}

// 处理所有道友悬赏情况结果
func (xd *XdDriver) CheckDemonLeagueAllPersonInfoData(allInfo D.DemonLeagueAllInfo) {
	for _, info := range allInfo.PersonInfo {
		//不攻击品级为天以下的
		if info.Grade < 0xF2 {
			continue
		} else if info.CombatValue*5 > xd.UserData.FightInfo.CombatValue*4 {
			log.Infof("妖盟悬赏- id:%s userID:%s [%s]的(%s) 妖力:%s 正在护送%s%02x", info.ID, info.UserID, info.LeagueName, info.Name, D.GetCombatValueDesc(info.CombatValue), D.GetDemonLeagueOfferGradeDesc(info.Grade), info.Grade)
			continue
		} else if xd.UserData.FightInfo.OfferCount >= 2 {
			xd.StopTaskByID(proto.S_UNION_BOUNTY_ENTRY_MAP)
			xd.StopTaskByID(proto.S_UNION_BOUNTY_REFRESH_MAP)
			return
		}
		xd.UserData.FightInfo.OfferCount++
		log.Infof("妖盟悬赏-开始挑战 id:%s userID:%s [%s]的(%s) 妖力:%s 正在护送%s%02x", info.ID, info.UserID, info.LeagueName, info.Name, D.GetCombatValueDesc(info.CombatValue), D.GetDemonLeagueOfferGradeDesc(info.Grade), info.Grade)
		//08 91 B7 AC C3 C0 5A 10 A2 C8 80 E3 F6 31 18 64
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_PLUNDER, fmt.Sprintf("08 %s 10 %s 18 64", info.UserID, info.ID)))
	}
	/* 尸熊 炼虚
	id:984E userID:EB86E2C4C05A [欧皇大仙] 正在护送0A
	藤条妖 合体
	id:9A4E userID:B4DAF9C4C05A [禄哥修士] 正在护送01
	祭司  合体
	id:9C4E userID:B4E6DEC4C05A [ღ�ღ] 正在护送04
	祭司 合体
	id:9C4E userID:EFE0D0C4C05A [夏候农辰天尊] 正在护送07
	藤条妖 大乘
	id:9D4E userID:ED9AD6C4C05A [阚瑛上仙] 正在护送05
	*/
	for num, info := range allInfo.MonsterInfo {
		if num < 2 {
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_MANAGE_TEAM, fmt.Sprintf("08 00 10 %s", info.UserID)))
			log.Infof("妖盟悬赏-协助【%s】镇压", info.Name)
		}
	}
}

// 处理妖盟-查看指定道友的信息结果
func (xd *XdDriver) CheckParaDemonLeaguePersonInfoData(info D.DemonLeaguePersonInfo) {

	//log.Infof("妖盟-悬赏挑战【%s】的道友【%s】", info.LeagueName, info.Name)
	//xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_Fight_The_Demon_League_One_Person_INFO_REQ, fmt.Sprintf("08 %s 10 %s 18 64", info.UserID, info.ID)))
}

// 查看妖盟悬赏怪物信息结果
func (xd *XdDriver) CheckDemonLeaguePersonInfoData(info D.DemonLeagueInfo) {
	if len(info.Person) >= 3 {
		log.Infof("妖盟悬赏-开始镇压")
		sendData := ""
		for _, person := range info.Person {
			sendData += fmt.Sprintf("0A 09 08 %s 10 %s", person.UserID, person.ID)
		}
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_ATTACK_MONSTER, fmt.Sprintf("%s 10 %s", sendData, info.ID)))
	} else if len(info.Person) > 0 {
		teamName := ""
		for _, person := range info.Person {
			teamName += fmt.Sprintf(" 【%s】", person.Name)
		}
		log.Infof("妖盟悬赏-邀请镇压,当前组队信息%s", teamName)
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_UNION_BOUNTY_ASK_HELP, "08 00"))
	}
}
